<template>
  <div class="edit-dialog">
    <el-dialog
      v-model="dialog"
      :title="titleName"
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :show-close="true"
      width="800px"
      top="7vh"
      center
      @close="handleClose"
    >
      <el-form :model="addFormData" :rules="rules" ref="formRef" label-position="right" label-width="auto">
        <el-form-item prop="deviceId" label="设备ID">
          <el-input v-model="addFormData.deviceId" maxlength="80" size="medium" placeholder="请输入设备ID" style="width: 100%" :disabled="type == 2"/>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="addFormData.name" maxlength="80" size="medium" placeholder="请输入设备名称" style="width: 100%" />
        </el-form-item>
        <el-form-item prop="type" label="设备类型">
          <el-radio-group v-model="addFormData.type" style="width: 100%" :disabled="type == 2">
            <el-radio label="IEQ">IEQ</el-radio>
            <el-radio label="PMS">PMS</el-radio>
            <el-radio label="OPT">OPT</el-radio>
            <el-radio label="ROBOT">ROBOT</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="inventoryState" label="设备状态" v-if="admin">
          <el-radio-group v-model="addFormData.inventoryState" style="width: 100%">
            <el-radio label="UNSHIPPED">在库</el-radio>
            <el-radio label="SHIPPED">出货</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备单位">
          <el-input v-model="addFormData.company" maxlength="80" placeholder="请输入设备单位" style="width: 100%" />
        </el-form-item>
        <el-form-item label="发货日期">
          <el-date-picker v-model="addFormData.deliveryTs" type="date" placeholder="请选择发货日期" style="width: 100%;"/>
        </el-form-item>
        <el-form-item label="SIM卡ID">
          <el-input v-model="addFormData.simId" maxlength="80" placeholder="请输入SIM卡ID" style="width: 100%" />
        </el-form-item>
        <el-form-item label="SIM卡运营商">
          <el-input v-model="addFormData.simIsp" maxlength="80" placeholder="请输入SIM卡运营商" style="width: 100%" />
        </el-form-item>
        <el-form-item label="SIM卡过期时间">
          <el-date-picker v-model="addFormData.simExpireTs" type="date" placeholder="请选择SIM卡过期时间" style="width: 100%;"/>
        </el-form-item>
        <el-form-item label="设备地址">
          <el-input v-model="addFormData.address" size="medium" maxlength="80" placeholder="请输入设备地址" style="width: 100%" />
        </el-form-item>
        <el-form-item label="设备描述">
          <el-input type="textarea" v-model="addFormData.description" :rows="2" maxlength="80" placeholder="请输入设备描述" style="width: 100%" />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="submitLoading" icon="el-icon-check" @click="submit('formRef')">提 交</el-button>
          <el-button  icon="el-icon-close" @click="handleClose">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add,edit } from '@/api/inventoryManage'
export default {
  name: 'deviceManage',
  props: {
    formData: Object,
    showDialog: Boolean,
    type: Number,
    company: ""
  },
  data() {
    return {
      admin: false,
      addFormData: {
        type: 'IEQ'
      },
      titleName: '编辑',
      submitLoading: false,
      rules: {
        deviceId: [{required: true, message: "请输入设备ID", trigger: "blur"}],
        type: [{required: true, message: "请选择设备类型", trigger: "blur"}]
      }
    }
  },
  computed: {
    dialog: function() {
      return this.showDialog
    }
  },
  watch: {
    dialog(newVal, oldVal) {
      if (newVal) {
        this.addFormData = this.formData;
        this.company = this.formData.creator.company;
        if(this.addFormData.deviceId != null){
          this.titleName = "编辑";
        }else{
          this.titleName = "添加";
        }
      }
    }
  },
  mounted() {
    if (this.$store.state.user.userInfo.role == "ADMIN") {
      this.admin = true;
    } else {
      this.admin = false;
    }
  },
  methods: {
    // 取消事件
    handleClose() {
      this.$emit('changeVisible', false)
    },
    submit(formName) {
      this.submitLoading = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.type == 1) {
            const data = {
              ...this.formData,
              deliveryTs: new Date(this.addFormData.deliveryTs).getTime(),
              simExpireTs: new Date(this.addFormData.simExpireTs).getTime()
            }
            add(data)
              .then(res => {
                this.submitLoading = false
                if (res['code'] == 200) {
                  this.$emit('changeVisible', false)
                  this.$emit('getList')
                  this.$message.success("添加成功!");
                } else {
                  this.$message(res['msg'])
                }
              })
              .catch(() => {
                this.submitLoading = false
              })
          } else {
            const data = {
              ...this.formData,
              deliveryTs: new Date(this.addFormData.deliveryTs).getTime(),
              simExpireTs: new Date(this.addFormData.simExpireTs).getTime()
            }
            edit(data)
              .then(res => {
                this.submitLoading = false
                if (res['code'] == 200) {
                  this.$emit('changeVisible', false)
                  this.$emit('getList')
                  this.$message.success("编辑成功!");
                } else {
                  this.$message(res['msg'])
                }
              })
              .catch(() => {
                this.submitLoading = false
              })
          }
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    }
  }
}
</script>

<style>
</style>
